import numpy
from pymoo.algorithms.soo.nonconvex.pso import PSO
from pymoo.core.problem import Problem
from pymoo.optimize import minimize
import mot.example. Function_gMOT_4beam as Function_gMOT_4beam
from  _logging import logger

class MyProblem(Problem):
    def __init__(self,):
        super().__init__(n_var=7,xl= [0.010,158,numpy.deg2rad(0),0.1,0.00,3600,0.05],xu= [0.020,178,numpy.deg2rad(90),0.7,0.01,3800,0.25])
    def _evaluate(self, x, out, *args, **kwargs):
        out["F"] = -numpy.vectorize(Function_gMOT_4beam.f,)(*x.T)

problem = MyProblem()
algorithm = PSO()
if __name__ == '__main__':
    res = minimize(problem,
                   algorithm,
                   seed=1,
                   save_history= True,
                   verbose=True)

    print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))


    import shelve
    # 将对象序列化到文件
    with shelve.open('MOT.db') as db:
        db['history'] = res
    # 从文件反序列化对象
    with shelve.open('MOT.db') as db:
        p_loaded = db['history']
